﻿using System.Threading.Tasks;
using Mars.GameCore;
using UnityEngine;

namespace Mars.Network
{
    public class TestNetManager : Singleton<TestNetManager>, IMessageHandler
    {
        public TestNetManager()
        {
            Register();
        }

        public override void Shutdown()
        {
            GameContext.NetworkManager.NetworkConnected -= OnNetworkConnected;
            GameContext.NetworkManager.NetworkClosed -= OnNetworkClosed;
            GameContext.NetworkManager.NetworkMissHeartBeat -= OnNetworkMissHeartBeat;
            GameContext.NetworkManager.NetworkError -= OnNetworkError;
        }
        
        public void Register()
        {
            ProtoMessageHandler.AddHandler<NotifyHeartBeat>(NotifyHeartBeat);
            ProtoMessageHandler.AddHandler<NotifyServerTime>(ResServerTime);
            
            GameContext.NetworkManager.NetworkConnected += OnNetworkConnected;
            GameContext.NetworkManager.NetworkClosed += OnNetworkClosed;
            GameContext.NetworkManager.NetworkMissHeartBeat += OnNetworkMissHeartBeat;
            GameContext.NetworkManager.NetworkError += OnNetworkError;
        }

        public async Task TestRpc()
        {
            var chanel = GameContext.NetworkManager.GetNetworkChannel("network");
            var reqTestRpc = new ReqTestRpc();
            var respPlayerLogin = await chanel.Call<ResTestRpc>(reqTestRpc);
            Debug.LogError($"收到RPC消息：{respPlayerLogin}");
        }
        
        /// <summary>
        /// 心跳
        /// </summary>
        /// <param name="msg"></param>
        private void NotifyHeartBeat(NotifyHeartBeat msg)
        {
            Debug.LogError($"收到心跳消息：{msg}");

            var chanel = GameContext.NetworkManager.GetNetworkChannel("network");
            var reqServerTime = new ReqServerTime();
            chanel.Send(reqServerTime);
        }

        /// <summary>
        /// 服务器时间
        /// </summary>
        /// <param name="msg"></param>

        private void ResServerTime(NotifyServerTime msg)
        {
            Debug.LogError($"收到服务器时间：{msg.Time}");
            if (msg.Time % 5 == 0)
            {
                CallRpc();
            }
        }
        
        
        private async void CallRpc()
        { 
            await TestRpc();
        }
        
        private void OnNetworkConnected(object sender, NetworkConnectedEventArgs eventArgs)
        {
            Debug.LogError("OnNetworkConnected " + eventArgs.UserData);
            GameContext.EventManager.Fire(this, eventArgs);
        }

        private void OnNetworkClosed(object sender, NetworkClosedEventArgs eventArgs)
        {
            Debug.LogError("OnNetworkClosed " + eventArgs.Id);
            GameContext.EventManager.Fire(this, eventArgs);
        }

        private void OnNetworkMissHeartBeat(object sender, NetworkMissHeartBeatEventArgs eventArgs)
        {
            Debug.LogError("OnNetworkMissHeartBeat " + eventArgs.MissCount);
            GameContext.EventManager.Fire(this, eventArgs);
        }

        private void OnNetworkError(object sender, NetworkErrorEventArgs eventArgs)
        {
            Debug.LogError("OnNetworkError " + eventArgs.ErrorMessage);
            GameContext.EventManager.Fire(this, eventArgs);
        }
    }
}